package roman.mosiienko.yalantistestrm.adapters;

import java.util.ArrayList;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import roman.mosiienko.yalantistestrm.R;
import roman.mosiienko.yalantistestrm.mymodels.Element;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {
	private Context context;
	private LayoutInflater layoutInflater;
	private ArrayList<Element> content;

	public MyAdapter(Context context, ArrayList<Element> content) {
		this.context = context;
		this.content = content;
		layoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {

		return content.size();
	}

	@Override
	public Element getItem(int position) {

		return content.get(position);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		
		
		if (convertView == null) {
			
			v = layoutInflater.inflate(R.layout.mycustomelement, parent, false);

			TextView title = (TextView) v.findViewById(R.id.textView1);
			TextView body = (TextView) v.findViewById(R.id.textView2);
			ImageView picture = (ImageView) v.findViewById(R.id.imageView1);
			
			title.setText(getItem(position).getTitle());
			body.setText(getItem(position).getBody());

			ImageLoader imageLoader = ImageLoader.getInstance();
			imageLoader.init(ImageLoaderConfiguration.createDefault(context));
			imageLoader.displayImage(getItem(position).getPicture(), picture);
		}

		return v;
	}
}
